# -*- coding: utf-8 -*-
# @Time    : 2021/11/21 13:55
# @Author  : super


from contextlib import contextmanager
import yaml

# 加上装饰器这个代码，那么下面函数创建的对象就是一个上下文管理器
@contextmanager
def my_open(file_name, file_mode):

    try:
        file = open(file_name, file_mode)
        # yield关键之前的代码可以认为是上文方法，负责返回操作对象资源
        yield file
    except Exception as e:
        print(e)
    finally:
        print("over")
        # yield关键字后面的代码可以任务是下文方法，负责释放操作对象的资源
        file.close()

if __name__ == '__main__':
    with my_open("student.yaml","r") as f:
	    print(yaml.safe_load(f))